 package com.yangguo.code.generator.util;



 import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.URLUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class My {

    public static void main(String[] args) throws Exception {

        getData();
        }

        public static String getData() {
            String url = "https://datahub.renniting.cn/apis/v1/parkings";
            String dataKey = "e115dfc1a0d24d04";

            // 发送的数据
            String json =
                "{\"abbrname\":\"北京按摩医院停车场\",\"address\":\"西城区宝产胡同7号\",\"ationname\":\">无\",\"bizspace\":12,\"contact\":\"王全杰\",\"dsspace\":1,\"exspace\":0,\"fastpile\":0,\"fullname\":\"北京按摩医院停车场\",\"id\":\"c87612a9-5aef-43fa-87b6-4d5f54fc41d1\",\"isname\":\"北京静态交通投资运营有限公司\",\"issn\":\"911M2K2819HM928\",\"lat\":39.9233,\"lng\":116.3605,\"muname\":\"长安商场运营商\",\"musn\":\"91110105801123721H\",\"nespace\":0,\"numentry\":2,\"numexit\":2,\"phone\":\"18310177792\",\"recordno\":\"西003G024\",\"slowpile\":0,\"smartlevel\":0,\"space\":17,\"street\":1,\"type\":0,\"updatetime\":1742872378}";
            // 对发送的数据进行加密处理
            String dataSign = MyEncryptionUtil.xiChengParamSign(json, dataKey);
            // 对发送的数据进行加密处理结束后替换字符
            String replaceDataSign = replaceStr(dataSign);
            // 业务参数封装
            HashMap<String, Object> sendMap = new HashMap<>();
            sendMap.put("data", replaceDataSign);
            // 发送参数
            String sendBody = JSONObject.toJSONString(sendMap);
            System.out.println(sendBody);
            return sendBody;
        }

        public static String creatPushUrlPath(String appKey, String secret) {
            Map<String, Object> pushMap = new TreeMap<>();
            pushMap.put("app_key", appKey);
            pushMap.put("app_uuid", "00-16-3E-32-0E-5D");
            pushMap.put("sig_method", "HMAC-SHA1");
            pushMap.put("req_uuid", UUID.randomUUID().toString().replace("-", ""));
            pushMap.put("timestamp", 1742872378);
            // 系统参数拼接
            String systemContent = getContent(pushMap);
            // 系统参数加密
            String systemSign = MyEncryptionUtil.xiChengSign(secret, systemContent);
            // 系统参数加密后的字符串进行替换"+"替换为"*"，"/"替换为"-"，"="去除。
            String systemReplace = replaceStr(systemSign);
            pushMap.put("signature", systemReplace);
            // 将加密数据进行路由拼接
            return URLUtil.buildQuery(pushMap, CharsetUtil.CHARSET_UTF_8);
        }

        public static String getContent(Map<String, Object> map) {
            return map.entrySet().stream().map(m -> m.getKey() + "=" + m.getValue()).collect(Collectors.joining("&"));
        }

        public static String replaceStr(String str) {
            return str.replace("+", "*").replace("/", "-").replace("=", "");
        }

        public static String decrypt(String encryptedParam, String dataKey) throws Exception {
            // 还原替换的字符
            String replacedParam = encryptedParam.replace("*", "+").replace("-", "/");
            // 根据 Base64 编码的特性，补齐缺失的 '='
            while (replacedParam.length() % 4 != 0) {
                replacedParam = replacedParam + "=";
            }

            // 进行 Base64 解码
            byte[] decodedData = Base64.getDecoder().decode(replacedParam);

            // 创建 AES 密钥规范
            SecretKeySpec keySpec = new SecretKeySpec(dataKey.getBytes(), "AES");
            // 获取 Cipher 实例，使用 AES/ECB/PKCS5Padding 模式
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            // 初始化解密模式
            cipher.init(Cipher.DECRYPT_MODE, keySpec);

            // 执行解密操作
            byte[] decryptedData = cipher.doFinal(decodedData);

            // 将解密后的字节数组转换为字符串
            return new String(decryptedData);
    }


}
